//package com.alibaba.cloud.base.filter;
//
//import com.alibaba.framework.base.jwt.JwtUtil;
//import com.alibaba.framework.exception.BusinessException;
//import com.alibaba.framework.tips.TipMess;
//import com.alibaba.framework.utils.StringUtil;
//import com.alibaba.framework.wrapper.RedisWrapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.util.Arrays;
//
////@WebFilter(value = "/*")
//public class AuthorizationFilter implements Filter {
//
//    // 白名单Urls
//    private static final String[] WHITELISTURLS = {"/user/loginUser","/user/register"};
//
//    @Override
//    public void init(FilterConfig filterConfig) {
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest servletRequest = (HttpServletRequest) request;
//        /*String contextPath = servletRequest.getServletPath();
//        if (Arrays.asList(WHITELISTURLS).contains(contextPath)) {
//            chain.doFilter(request, response);
//            return;
//        }*/
//
//        String authorization = servletRequest.getHeader("Authorization");
//        if (!StringUtil.isEmpty(authorization) && authorization.startsWith("Bearer")) {
//            String token = authorization.substring(7);
//            if (JwtUtil.verifyToken(token)) {
//                String username = JwtUtil.getClaim(token, "username", String.class);
//                String password = JwtUtil.getClaim(token, "password", String.class);
//                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
//                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//            } else {
//                // TODO filter中的异常待处理
//                throw new BusinessException(TipMess.BASE_TOKEN_FAIL_VERIFY);
//            }
//        }
//        chain.doFilter(request, response);
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//}
